(function() {
    "use strict";
    var app = angular.module("angularApp");
    app.controller('clientCardCtrl', function ($scope, $http,$uibModal,fac) {
        $scope.search = {};
        $scope.pageModel = {};
        $scope.find = function(pageNo){
            $.extend($scope.search,{currentPage:pageNo||$scope.pageModel.currentPage||1,pageSize:$scope.pageModel.pageSize||10});
            fac.getPageResult("../clientCard/list",$scope.search,function(data){
                $scope.pageModel = data;
            });
        };
        $scope.find();

        $scope.uploadCard = function(){
            var modal = $uibModal.open({
                animation: false,
                size:'',
                templateUrl: 'view/clientCard/clientCard.upload.html',
                controller: 'uploadCardCtrl'
                //  ,resolve: {item: angular.extend({},style,{'sceneId':sceneId})}
            });
            modal.result.then(function () {
                $scope.find();
                //$rootScope.elementEnum && $rootScope.elementEnum.forEach(function(n){ n.pageModel && delete n.pageModel.data});
            }, function () {
                console.info('Modal dismissed at: ' + new Date());
            });
        };

        $scope.uploadCancels = function (params) {
            fac.upload({ url: "/clientCard/uploadCancels" ,multiple:false,params:params,accept:"application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }, function (resp) {
                if (resp.code == 0 ) {
                    confirm("退保成功"+resp.data.finished+"条记录！忽略了"+resp.data.ignored+"条记录！已退保"+resp.data.existed+"条记录！");
                    $scope.find(1);
                } else {
                    alert(resp.msg);
                }
            })
        }

        $scope.uploadCostDetail = function(){
            var modal = $uibModal.open({
                animation: false,
                size:'',
                templateUrl: 'view/clientCard/costDetail.upload.html',
                controller: 'uploadCostDetailCtrl'
                //  ,resolve: {item: angular.extend({},style,{'sceneId':sceneId})}
            });
            modal.result.then(function () {
                $scope.find(1);
                //$rootScope.elementEnum && $rootScope.elementEnum.forEach(function(n){ n.pageModel && delete n.pageModel.data});
            }, function () {
                console.info('Modal dismissed at: ' + new Date());
            });
        };
    });

    app.controller('uploadCardCtrl', function($scope,$http,$uibModalInstance,fac) {
        $scope.item = {};

        $scope.save = function (form) {
            form.$setSubmitted(true);
            if (!form.$valid) {
                return;
            }
            if(!$("#xmlInput").val()){
                alert("请选择上传名单！")
                return false;
            }
            var index = layer.load(1, {
                shade: [0.2, '#000'] //0.1透明度的白色背景
            });
            $("iframe[name='iframe_0']").on("load",function() {
                layer.close(index);
                var contents = $(this).contents().get(0);
                var data = $(contents).find('body').text();
                try{
                    data = window.eval('(' + data + ')');
                }catch(e){
                    console.log(data)
                    if(data ==""){
                        data = "请重新登录!";
                    }
                    data = {code:403,msg:data}
                }
                if (data.code === 0) {
                    $uibModalInstance.close();
                    confirm("成功上传"+data.data.inserted+"条记录！忽略了"+data.data.ignored+"条记录！");
                } else if(data.code === 2){
                    var message ="";
                    data.data.forEach(function(n){
                        message +="<p>"+n+"</p>";
                    })
                    layer.confirm(message, { btn: ['确定'], title: data.msg }, function (index) {
                        layer.close(index);
                    }, function () {
                    });

                } else{
                    alert(data.msg);
                }

            });
            $("form[name='form']").submit();

        }
    });

    app.controller('uploadCostDetailCtrl', function($scope,$http,$uibModalInstance,fac) {
        $scope.item = {};

        $scope.save = function (form) {
            form.$setSubmitted(true);
            if (!form.$valid) {
                return;
            }
            if(!$("#xmlInput").val()){
                alert("请选择消费明细！")
                return false;
            }
            var index = layer.load(1, {
                shade: [0.2, '#000'] //0.1透明度的白色背景
            });
            $("iframe[name='iframe_0']").on("load",function() {
                layer.close(index);
                var contents = $(this).contents().get(0);
                var data = $(contents).find('body').text();
                try{
                    data = window.eval('(' + data + ')');
                }catch(e){
                    console.log(data)
                    if(data ==""){
                        data = "请重新登录!";
                    }
                    data = {code:403,msg:data}
                }
                if (data.code === 0) {
                    $uibModalInstance.close();
                    confirm("成功上传"+data.data.inserted+"条记录！修改了"+data.data.modified+"条记录！忽略了"+data.data.ignored+"条记录！");
                } else if(data.code === 2){
                    var message ="";
                    data.data.forEach(function(n){
                        message +="<p>"+n+"</p>";
                    })
                    layer.confirm(message, { btn: ['确定'], title: data.msg }, function (index) {
                        layer.close(index);
                    }, function () {
                    });

                } else{
                    alert(data.msg);
                }

            });
            $("form[name='form']").submit();
        }
    });




})();